﻿using System.Drawing;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The pan tool, clic and drag to drag the scene
    /// </summary>
    public class TPan:BaseTool
    {
        #region Fields
        bool _MouseDown;
        Point _PreviousMouseLocation;
        Point2D64D Translation ;
        RectangleD CurrentObjectView;
        #endregion
        
        #region Constructor
        /// <summary>
        /// The pan tool, clic and drag to drag the scene
        /// </summary>
        /// <param name="Viewport">The viewport the tool with act on</param>
        public TPan(BaseViewport Viewport)
            : base(Viewport,"GeoPan")
        {
            _MouseDown = false;
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            _MouseDown = true;
            Viewport.DoubleBuffereEnabled = true;
            _PreviousMouseLocation = MouseArgs.Location;
        }

        /// <summary>
        /// The method called when the mouse button is up
        /// </summary>
        public override void MouseUp(MouseEventArgs MouseArgs)
        {
            _MouseDown = false;
            Viewport.DoubleBuffereEnabled = false;
            RefreshViewport();
        }

        /// <summary>
        /// The method called when the mouse moves
        /// </summary>
        public override void MouseMove(MouseEventArgs MouseArgs)
        {
            if (_MouseDown)
            {
                Translation = Viewport.Object2ImageTransformation.InverseTransform((Point2D64D)_PreviousMouseLocation) +
                    -Viewport.Object2ImageTransformation.InverseTransform((Point2D64D)MouseArgs.Location);
                CurrentObjectView = Viewport.CurrentObjectView;
                CurrentObjectView.X += Translation.X;
                CurrentObjectView.Y += Translation.Y;
                Viewport.CurrentObjectView = CurrentObjectView;
                _PreviousMouseLocation = MouseArgs.Location;
                RefreshViewport();
            }
        } 
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click and drag to move the current view");
        } 
        #endregion
    }
}
